package com.cne.gmail.Gmail.es;
import com.google.common.collect.Lists;

import com.cne.gmail.Gmail.response.GmailServiceResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@Api(description ="全文搜索接口")
@Log4j2
@RequestMapping("es")
public class esController {
    @Autowired
    PostService postService;
    @Autowired
    PostRepository postRepository;
    /**
     * 单字符串模糊查询，默认排序。将从所有字段中查找包含传来的word分词后字符串的数据集
     */
    @ApiOperation(value = "输入字段查询",notes = "输入字段查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "word",value = "输入字段",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "page",value = "当前页",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "size",value = "条数",required = true,dataType = "String")
    })
    @GetMapping("search")
    public GmailServiceResponse search(@RequestParam String word,@RequestParam String page,@RequestParam String size) {
        GmailServiceResponse response=new GmailServiceResponse();
        /*try {
            Page<EsEmail> search = postService.search(word, Integer.valueOf(page), Integer.valueOf(size));
            response.setCode(200);
            response.setSuccess(true);
            response.setData(search);
            return response;
        }catch (Exception e){
            log.info(e.getMessage());
            response.setSuccess(false);
            response.setMsg("查询错误");
            return response;
        }*/
        Page<EsEmail> search = postService.search(word, Integer.valueOf(page), Integer.valueOf(size));
        response.setCode(200);
        response.setSuccess(true);
        response.setData(search);
        return response;
    }
    @GetMapping("save")
    public void save(@RequestParam String subject,@RequestParam String date){
        EsEmail esEmail=new EsEmail();
        esEmail.setEmailAddress("zhangxuan");
        esEmail.setUserName("zhang");
        esEmail.setFrom("804591870");
        esEmail.setDate(date);
        esEmail.setSubject(subject);
        try {
            postRepository.save(esEmail);
        }catch (Exception e){
            log.info(e.getMessage());
        }
    }
    @DeleteMapping("delete")
    @ApiOperation(value = "删除",notes = "删除")
    public void delte(){
        postRepository.deleteAll();
    }

}
